<template>
  <div class="app-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-page-header @back="goBack" :content="pageTitle"></el-page-header>
      </div>
      
      <!-- 查询条件 -->
      <el-form :inline="true" class="demo-form-inline" style="margin-bottom: 20px;">
        <el-form-item label="设备ID">
          <el-input v-model="queryParams.deviceId" placeholder="请输入设备ID" clearable></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
            <el-option label="已解决" value="RESOLVED"></el-option>
            <el-option label="未解决" value="UNRESOLVED"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery" :loading="loading">查询</el-button>
        </el-form-item>
      </el-form>

      <!-- 表格 -->
      <el-table
        v-loading="loading"
        :data="faultList"
        style="width: 100%"
        border>
        <el-table-column
          prop="deviceId"
          label="设备ID"
          width="180">
        </el-table-column>
        <el-table-column
          prop="faultTime"
          label="故障时间"
          width="180">
        </el-table-column>
        <el-table-column
          prop="faultDescription"
          label="故障描述">
        </el-table-column>
        <el-table-column
          prop="status"
          label="状态"
          width="100">
          <template slot-scope="scope">
            <el-tag :type="scope.row.status === 'RESOLVED' ? 'success' : 'danger'">
              {{ scope.row.status === 'RESOLVED' ? '已解决' : '未解决' }}
            </el-tag>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination-container" style="margin-top: 20px;">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryParams.pageNum"
          :page-sizes="[10, 20, 30, 50]"
          :page-size="queryParams.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
import { getFaultDetails } from "@/api/equipment/analysis";

export default {
  name: "FaultDetail",
  data() {
    return {
      loading: false,
      faultList: [],
      total: 0,
      deviceType: '',
      faultType: '',
      startDate: '',
      endDate: '',
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deviceId: '',
        status: ''
      }
    };
  },
  computed: {
    pageTitle() {
      const deviceTypeMap = {
        'STACKER': '堆垛机',
        'CONVEYOR': '输送机',
        'SHUTTLE': '穿梭车'
      };
      return `${deviceTypeMap[this.deviceType]} - ${this.faultType} 故障详情`;
    }
  },
  created() {
    this.initData();
  },
  methods: {
    initData() {
      const { deviceType, faultType, startDate, endDate } = this.$route.query;
      this.deviceType = deviceType;
      this.faultType = faultType;
      this.startDate = startDate;
      this.endDate = endDate;
      this.getFaultDetails();
    },
    async getFaultDetails() {
      this.loading = true;
      try {
        const params = {
          ...this.queryParams,
          startDate: this.startDate,
          endDate: this.endDate,
          faultType: this.faultType
        };
        const response = await getFaultDetails(this.deviceType, params);
        if (response.code === 200) {
          this.faultList = response.data;
          this.total = response.total || this.faultList.length;
        } else {
          this.$message.error(response.msg || '获取故障详情失败');
        }
      } catch (error) {
        console.error('获取故障详情失败:', error);
        this.$message.error('获取故障详情失败');
      } finally {
        this.loading = false;
      }
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getFaultDetails();
    },
    handleSizeChange(val) {
      this.queryParams.pageSize = val;
      this.getFaultDetails();
    },
    handleCurrentChange(val) {
      this.queryParams.pageNum = val;
      this.getFaultDetails();
    },
    goBack() {
      this.$router.go(-1);
    }
  }
};
</script>

<style scoped>
.el-page-header {
  margin-bottom: 20px;
}
.pagination-container {
  text-align: right;
}
</style> 